<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="生态箱号" prop="caseNumber">
        <el-select v-model="queryParams.caseList" multiple placeholder="请选择">
          <el-option
            v-for="dict in dict.type.qs_case_number"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['realTimeInformation:realTimeInformation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="realTimeInformationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="箱号" align="center" prop="caseNumber" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="温度(℃)" align="center" prop="temperatureNum" />
      <el-table-column label="湿度(%)" align="center" prop="humidityNum" />
      <el-table-column label="氨气(ppm)" align="center" prop="ammoniaNum" />
      <el-table-column label="出料实际值(g)" align="center" prop="finishedWeight" />
      <el-table-column label="投料盒数" align="center" prop="feedBoxNum" />
      <el-table-column label="投虫盒数" align="center" prop="wormBoxNum" />
      <el-table-column label="翻盒盒数" align="center" prop="finishedBoxNum" />
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改RealTimeInformation对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="温度(℃)" prop="temperatureNum">
          <el-input v-model="form.temperatureNum" placeholder="请输入温度(℃)" />
        </el-form-item>
        <el-form-item label="湿度(%)" prop="humidityNum">
          <el-input v-model="form.humidityNum" placeholder="请输入湿度(%)" />
        </el-form-item>
        <el-form-item label="氨气(ppm)" prop="ammoniaNum">
          <el-input v-model="form.ammoniaNum" placeholder="请输入氨气(ppm)" />
        </el-form-item>
        <el-form-item label="出料实际值(g)" prop="finishedWeight">
          <el-input v-model="form.finishedWeight" placeholder="请输入出料实际值(g)" />
        </el-form-item>
        <el-form-item label="投料盒数" prop="feedBoxNum">
          <el-input v-model="form.feedBoxNum" placeholder="请输入投料盒数" />
        </el-form-item>
        <el-form-item label="投虫盒数" prop="wormBoxNum">
          <el-input v-model="form.wormBoxNum" placeholder="请输入投虫盒数" />
        </el-form-item>
        <el-form-item label="翻盒盒数" prop="finishedBoxNum">
          <el-input v-model="form.finishedBoxNum" placeholder="请输入翻盒盒数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addRealTimeInformation,
  delRealTimeInformation,
  getRealTimeInformation,
  listRealTimeInformation,
  updateRealTimeInformation
} from "@/api/realTimeInformation/realTimeInformation";

export default {
  name: "RealTimeInformation",
  dicts: ['qs_case_number'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // RealTimeInformation表格数据
      realTimeInformationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createTime: [],
        caseList:["1"]
      },
      caseNumberOptions:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        temperatureNum: [
          { required: true, message: "温度(℃)不能为空", trigger: "blur" }
        ],
        humidityNum: [
          { required: true, message: "湿度(%)不能为空", trigger: "blur" }
        ],
        ammoniaNum: [
          { required: true, message: "氨气(ppm)不能为空", trigger: "blur" }
        ],
        finishedWeight: [
          { required: true, message: "出料实际值(g)不能为空", trigger: "blur" }
        ],
        feedBoxNum: [
          { required: true, message: "投料盒数不能为空", trigger: "blur" }
        ],
        wormBoxNum: [
          { required: true, message: "投虫盒数不能为空", trigger: "blur" }
        ],
        finishedBoxNum: [
          { required: true, message: "翻盒盒数不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询RealTimeInformation列表 */
    getList() {
      this.loading = true;
      this.queryParams.startTime = this.queryParams.createTime[0];
      this.queryParams.endTime = this.queryParams.createTime[1];
      listRealTimeInformation(this.queryParams).then(response => {
        this.realTimeInformationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        temperatureNum: null,
        humidityNum: null,
        ammoniaNum: null,
        finishedWeight: null,
        feedBoxNum: null,
        wormBoxNum: null,
        finishedBoxNum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加RealTimeInformation";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRealTimeInformation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改RealTimeInformation";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRealTimeInformation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRealTimeInformation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除RealTimeInformation编号为"' + ids + '"的数据项？').then(function() {
        return delRealTimeInformation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('realTimeInformation/realTimeInformation/export', {
        ...this.queryParams
      }, `realTimeInformation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
